//
//  DetailPinDanViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "DetailPinDanViewController.h"
#import "DetailPinDanCell.h"
#import "DetailPinDanData.h"
#import "ShareMyPinDanViewController.h"
@interface DetailPinDanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)DetailPinDanData *data;
@property(nonatomic,strong)UIButton *bottomBtn;
@end

@implementation DetailPinDanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"拼单详情";
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.view.backgroundColor=[UIColor whiteColor];

    [self setUpTableView];
    [self setUpBottomButton];
}
#pragma mark--底部按钮
-(void)setUpBottomButton
{
    _bottomBtn=[[UIButton alloc]initWithFrame:CGRectMake(10, kHeight-40-10, kWidth-20, 40)];
    [_bottomBtn setTitle:@"发起拼单" forState:UIControlStateNormal];
    [_bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _bottomBtn.layer.cornerRadius=4;
    [_bottomBtn setClipsToBounds:YES];
    _bottomBtn.backgroundColor=BLUECOLOR;
    [_bottomBtn addTarget:self action:@selector(SendPinDanWithIsClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_bottomBtn];
    
}
//发起拼单
-(void)SendPinDanWithIsClick
{
    //http://192.168.1.107:3722/Activity/SubmitSpell
    //m_SubmitSpellPara(UserCode,SpellBaseID,Timestamp,Sign)
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSString *spellID=[NSString stringWithFormat:@"%ld",(long)_spellID];
    NSDictionary *signDic=@{@"UserCode":GETUSERCODE
                            ,@"SpellBaseID":spellID,
                            @"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:signDic];
    NSDictionary *dataDic=@{@"UserCode":GETUSERCODE
                            ,@"SpellBaseID":spellID,
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
    NSDictionary *dic=@{@"m_SubmitSpellPara":dataDic};
    [RequestTool requestDataWithType:@"post" andUrlString:@"Activity/SubmitSpell" andParameters:dic andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"连接服务器失败"];
        }
        if ([responses[@"IsSuccess"]integerValue]==1)
        {
            NSDictionary *dic=responses[@"ReturnObject"];
                ShareMyPinDanViewController *sharePin=[[ShareMyPinDanViewController alloc]init];
                sharePin.remaindSeconds=dic[@"RemainderSeconds"];
                sharePin.endTime=dic[@"EndDate"];
                sharePin.spellID=dic[@"SpellUserID"];
                sharePin.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:sharePin animated:YES];
        }else
        {
            [MBProgressHUD showError:@"请求发送失败，请稍后再试～"];
        }
    }];

}

#pragma mark--表
-(void)setUpTableView
{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64-70) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [_tableView registerNib:[UINib nibWithNibName:@"DetailPinDanCell" bundle:nil] forCellReuseIdentifier:@"detail"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:_tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 300;
    }else
    {
        CGFloat height=[ZMTool getLabelHeight:_data.SpellContent font:[UIFont systemFontOfSize:13] width:kWidth-10];
        return height+25;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        return 30;
    }
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, kWidth, 20)];
        lbl.backgroundColor=[UIColor colorWithRed:246/255.0 green:246/255.0  blue:246/255.0  alpha:0.7];
        lbl.font=[UIFont systemFontOfSize:13];
        lbl.textColor=[UIColor lightGrayColor];
        lbl.text=@"详细介绍";
        return lbl;
    }
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        DetailPinDanCell *detail=[_tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
        detail.aboutJingJingBtn.hidden=YES;
        detail.selectionStyle=UITableViewCellSelectionStyleNone;
        [detail.image sd_setImageWithURL:[NSURL URLWithString:_data.DetailImgUrl] placeholderImage:nil options:SDWebImageRetryFailed];
        detail.title.text=_data.SpellName;
        detail.peopleNum.text=[NSString stringWithFormat:@"%ld人",(long)_data.NeedCount];
        detail.require.text=_data.SpellRequire;
        NSArray *Arr=_data.SpellStepList;
        for (NSDictionary *dic in Arr) {
            NSString *step=dic[@"SpellStepID"];
            switch ([step integerValue]) {
                case 1:
                    detail.need1.text=[NSString stringWithFormat:@"①%@",dic[@"SpellStepName"]];
                    break;
                case 2:
                    detail.need2.text=[NSString stringWithFormat:@"②%@",dic[@"SpellStepName"]];
                    break;
                case 3:
                    detail.need3.text=[NSString stringWithFormat:@"③%@",dic[@"SpellStepName"]];
                    break;
                case 4:
                    detail.need4.text=[NSString stringWithFormat:@"④%@",dic[@"SpellStepName"]];
                    break;
                default:
                    break;
            }        }
                return detail;
    }else
    {
        UITableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        CGFloat height=[ZMTool getLabelHeight:[_data.SpellContent stringByRemovingPercentEncoding] font:[UIFont systemFontOfSize:13] width:kWidth-10];
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(5, 10, kWidth-10, height+5)];
        lbl.textColor=[UIColor lightGrayColor];
        lbl.text=_data.SpellContent;
        lbl.numberOfLines=0;
        lbl.font=[UIFont systemFontOfSize:13];
        [cell.contentView addSubview:lbl];
        return cell;
    }
    
}
#pragma mark--请求数据
-(void)getDataOfDetailPinDan
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  //http://192.168.1.107:3722/Activity/SpellDetail/{UserCode}/{SpellBaseID}/{Timestamp}/{Sign}
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSString *spellId=[NSString stringWithFormat:@"%ld",(long)_spellID];
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,@"SpellBaseID":spellId,@"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Activity/SpellDetail/%@/%@/%@/%@",GETUSERCODE,spellId,timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"请求失败，请检查网络"];
        }
        if ([responses[@"IsSuccess"]intValue]==1)
        {
            _data=[[DetailPinDanData alloc]initWithDic:responses[@"ReturnObject"]];
            if (_data.IsJoinSpell==1) {
                [_bottomBtn setTitle:@"已参与" forState:UIControlStateNormal];
                _bottomBtn.backgroundColor=GRAYCOLOR;
                _bottomBtn.enabled=NO;
            }
            [_tableView reloadData];
        }
        
        [MBProgressHUD hideHUDForView:self.view];
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getDataOfDetailPinDan];
    [GeneralTool hideTabBar];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
